#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QTimer::singleShot(0, this, SLOT(openDialog()));
    qDebug() << "end of constructor";
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::verifyAccess(QString u, QString p)
{
    // verify in database
    if (u == "usuario" && p == "password")
        return true;
    else
        return false;
}

void Widget::openDialog()
{
    // here dialog windows is moving
//    Dialog *d;
//    d = new Dialog(this);
//    d->setModal(true);
    int r = -1;
    int i = -1;
    QString user = "";
    QString pass = "";


    while(!verifyAccess(user, pass))
    {
        Dialog d;   // here dialog window is static
        d.setModal(true);
        i++;
        if (i != 0) d.setMessage("error...");
        qDebug() << d.getUser() << " - " << d.getPassword();
        d.setFocusPassword();
        r = d.exec();
        if (r == QDialog::Rejected)
        {
            qDebug() << "r :" << r;
            this->close();
            break;
        }
        user = d.getUser();
        pass = d.getPassword();
    }
}

void Widget::on_pushButton_clicked()
{
    close();
}
